// pages/personal/manageAdress/index/index.js
const app = getApp()
import api from '../../../../utils/api'
let apis = api.api
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		checked: null,
		isDefault: true,
		defaultId: '',
		userAdress: [],
		userId: [],
		default: 0,
	},
	onChange(event) {
		console.log('event', event)
		var that = this;
		wx.showModal({
			title: '提示',
			content: '是否改为默认地址？',
			success: function(res) {
				// 修改地址接口
				// https://dageda.api.hjdz.ltd/member/edit_address
				let url = apis.host + 'member/edit_address';
				let data = {
					// mobile:'',			/* 是	string	无 */
					// address:'',			/* 是	string	无 */
					// name:'',			/* 是	string	无 */
					is_default: 1,
					/* 否	string	默认0 0非默认 1默认地址 */
					id: event.detail,
					/* 是	string	地址ID */
				};
				apis.request(url, data, 'POST').then(function(res) {
					wx.showToast({
						title: '修改成功',
						icon: 'success'
					})
					that.setData({
						defaultId: event.detail
					})
					wx.navigateTo({
						url:'/pages/personal/manageAdress/index/index'
					})
				}).catch(function(res) {
					wx.showToast({
						title: '修改失败，请重试',
						icon: 'none'
					})
					wx.navigateTo({
						url:'/pages/personal/manageAdress/index/index'
					})
				})
			},
			fail: function(res) {
				wx.showToast({
					title: '更改失败',
					icon: 'none'
				})
			}
		})
	},
	editAdr(e) {
		let adressId = parseInt(e.currentTarget.dataset.index)
		let userName = e.currentTarget.dataset.name
		let userMobile = e.currentTarget.dataset.mobile
		let userAdress = e.currentTarget.dataset.address
		let defaults = e.currentTarget.dataset.default
		wx.navigateTo({
			url: '/pages/personal/manageAdress/editAdress/editAdress?adressId=' + adressId +
				'&userName=' + userName + '&userMobile=' + userMobile + '&default=' + defaults +
				'&address=' + userAdress,
		})
	},
	delAdr(e) {
		// https://dageda.api.hjdz.ltd/member/del_address
		let that = this;
		let url = apis.host + 'member/del_address';
		let data = {
			id: parseInt(e.currentTarget.dataset.index),
			/* 是	string	地址ID */
		};
		apis.request(url, data, 'POST').then(function(res) {
			wx.showToast({
				title: '删除成功',
				icon: 'success',
				fail(res) {
					console.log(res)
				}
			})
			setTimeout(function() {
				wx.navigateTo({
					url: '/pages/personal/manageAdress/index/index',
				})
			}, 2000);
		}).catch(function(res) {
			console.log('删除失败', res)
		})
	},
	addAdr() {
		wx.navigateTo({
			url: '/pages/personal/manageAdress/addAdress/addAdress',
		})
	},
	getData() {
		let that = this
		// https://dageda.api.hjdz.ltd/member/address
		let url = apis.host + 'member/address';
		let data = {}
		apis.request(url, data, 'POST').then(function(res) {
			let data = res.data;
			that.setData({
				userAdress: data
			})
			for (var i = 0; i < data.length; i++) {
				// that.data.userId.push(data[i].id)
				if (data[i].is_default == 1) {
					that.setData({
						defaultId: data[i].id
					})
				}
			}
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var that = this;
		that.getData()
	},
})
